package com.work.libtest2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.libtest2.entity.ExtraWorkType;
import com.work.libtest2.mapper.ExtraWorkTypeMapper;
import com.work.libtest2.service.ExtraWorkTypeService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: ExtraWorkTypeServiceIml
 * Package: com.work.libtest2.service.impl
 * Description:
 *
 * @Author
 * @Create 2024/6/10 14:38
 * @Version 1.0
 */
@Service
public class ExtraWorkTypeServiceIml extends ServiceImpl<ExtraWorkTypeMapper, ExtraWorkType> implements ExtraWorkTypeService{

    @Resource
    private ExtraWorkTypeMapper extraWorkTypeMapper;
    /**
     * 分页查询
     * */
    @Override
    public IPage<ExtraWorkType> getPage(Integer currentPage, Integer pageSize,ExtraWorkType extraWorkType) {
        LambdaQueryWrapper<ExtraWorkType> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(extraWorkType.getEwTypeName()),ExtraWorkType::getEwTypeName,extraWorkType.getEwTypeName());
        IPage page = new Page(currentPage,pageSize);
        extraWorkTypeMapper.selectPage(page,lqw);
        List<ExtraWorkType> list = new ArrayList<>();
        for (int i = 0; i < page.getRecords().size(); i++) {
            ExtraWorkType workType = new ExtraWorkType();
            BeanUtils.copyProperties(page.getRecords().get(i),workType);
            workType.setEwPay("￥ "+workType.getEwPay());
            list.add(workType);
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public int addEWType(ExtraWorkType extraWorkType) {
        return extraWorkTypeMapper.insert(extraWorkType);
    }

    @Override
    public ExtraWorkType getEWById(Integer ewTypeId) {
        return extraWorkTypeMapper.selectById(ewTypeId);
    }

    @Override
    public int updateEWById(ExtraWorkType extraWorkType) {
        return extraWorkTypeMapper.updateById(extraWorkType);
    }

    @Override
    public int deleteById(Integer ewTypeId) {
        return extraWorkTypeMapper.deleteById(ewTypeId);
    }

    @Override
    public List<ExtraWorkType> getAll() {
        return extraWorkTypeMapper.selectList(null);
    }
}
